import { createElement } from "../../../utils";
import { textTemplateData } from "../../../const";
import BaseNode from "../../BaseNode";

export default class TextNode extends BaseNode {
  element = createElement("div");
  protected _nodeType: VoucherNodeType = "text";
  protected _options: VoucherTextTemplate = textTemplateData()

  get options() {
    return { ...this._options };
  }


  constructor({ name, content, fontFamily, fontSize, bold, x, y, letterSpacing, lineHeight, color }: VoucherTextTemplate, isRootNode = true) {
    super(isRootNode);
    if (isRootNode) this.element.style.position = "absolute";
    this.setOptions({ name, content, fontFamily, fontSize, bold, x, y, letterSpacing, lineHeight, color });
  }


  private render() {
    const { content, fontFamily, fontSize, bold, letterSpacing, lineHeight, x, y, color } = this.options
    if (this.isRootNode) {
      this.element.style.left = `${x}px`;
      this.element.style.top = `${y}px`;
    }
    this.element.innerHTML = content;
    this.element.style.fontFamily = fontFamily;
    this.element.style.fontSize = `${fontSize}px`;
    this.element.style.fontWeight = bold ? "bold" : "normal";
    this.element.style.letterSpacing = `${letterSpacing}px`;
    this.element.style.lineHeight = String(lineHeight);
    this.element.style.color = color;
  }

  public setOptions({ name, content, fontFamily, fontSize, bold, x, y, letterSpacing, lineHeight, color }: Partial<VoucherTextTemplate>) {
    if (name !== undefined) this._options.name = name
    if (content !== undefined) this._options.content = content
    if (fontFamily !== undefined) this._options.fontFamily = fontFamily
    if (fontSize !== undefined) this._options.fontSize = fontSize
    if (bold !== undefined) this._options.bold = bold
    if (x !== undefined) this._options.x = x
    if (y !== undefined) this._options.y = y
    if (letterSpacing !== undefined) this._options.letterSpacing = letterSpacing
    if (lineHeight !== undefined) this._options.lineHeight = lineHeight
    if (color !== undefined) this._options.color = color

    this.render()
  }

  public getOptions(): VoucherTextTemplate {
    return this.options
  }
}